//
//  PersonalViewController.m
//  DropDownListProject
//
//  Created by 花若似相离 on 2018/3/15.
//  Copyright © 2018年 YangYang. All rights reserved.
//

#import "PersonalViewController.h"
#import "YCXMenu.h"

@interface PersonalViewController ()
@property (nonatomic , strong) NSMutableArray *items;

@end

@implementation PersonalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addNotificationCenter];
    [self setupNavItem];
    [self setupUI];
}

-(void)addNotificationCenter
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuWillAppear) name:YCXMenuWillAppearNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuDidAppear) name:YCXMenuDidAppearNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuWillDisappear) name:YCXMenuWillDisappearNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuDidDisappear) name:YCXMenuDidDisappearNotification object:nil];
}

-(void)setupUI
{
    UIButton *leftTopAddAction = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftTopAddAction setTitle:@"+" forState:UIControlStateNormal];
    leftTopAddAction.titleLabel.font = [UIFont systemFontOfSize:15];
    [leftTopAddAction setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [leftTopAddAction addTarget:self action:@selector(leftTopAddActionClicked:) forControlEvents:UIControlEventTouchUpInside];
    leftTopAddAction.layer.borderColor = [UIColor grayColor].CGColor;
    leftTopAddAction.layer.borderWidth = 0.3;
    leftTopAddAction.titleLabel.font = [UIFont systemFontOfSize:30];
    [self.view addSubview:leftTopAddAction];
    [leftTopAddAction mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(20);
        make.top.mas_equalTo(self.view.mas_top).offset(64 + 20);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(40);
    }];
    
    
    UIButton *rightTopAddAction = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightTopAddAction setTitle:@"+" forState:UIControlStateNormal];
    rightTopAddAction.titleLabel.font = [UIFont systemFontOfSize:15];
    [rightTopAddAction setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [rightTopAddAction addTarget:self action:@selector(rightTopAddActionClicked:) forControlEvents:UIControlEventTouchUpInside];
    rightTopAddAction.layer.borderColor = [UIColor grayColor].CGColor;
    rightTopAddAction.layer.borderWidth = 0.3;
    rightTopAddAction.titleLabel.font = [UIFont systemFontOfSize:30];
    [self.view addSubview:rightTopAddAction];
    [rightTopAddAction mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.view.mas_right).offset(-20);
        make.top.mas_equalTo(self.view.mas_top).offset(64 + 20);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(40);
    }];
    
    UIButton *leftBottomAddAction = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBottomAddAction setTitle:@"+" forState:UIControlStateNormal];
    leftBottomAddAction.titleLabel.font = [UIFont systemFontOfSize:15];
    [leftBottomAddAction setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [leftBottomAddAction addTarget:self action:@selector(leftBottomAddActionClicked:) forControlEvents:UIControlEventTouchUpInside];
    leftBottomAddAction.layer.borderColor = [UIColor grayColor].CGColor;
    leftBottomAddAction.layer.borderWidth = 0.3;
    leftBottomAddAction.titleLabel.font = [UIFont systemFontOfSize:30];
    [self.view addSubview:leftBottomAddAction];
    [leftBottomAddAction mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(20);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-20);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(40);
    }];
    
    UIButton *righttBottomAddAction = [UIButton buttonWithType:UIButtonTypeCustom];
    [righttBottomAddAction setTitle:@"+" forState:UIControlStateNormal];
    righttBottomAddAction.titleLabel.font = [UIFont systemFontOfSize:15];
    [righttBottomAddAction setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [righttBottomAddAction addTarget:self action:@selector(rightBottomAddActionClicked:) forControlEvents:UIControlEventTouchUpInside];
    righttBottomAddAction.layer.borderColor = [UIColor grayColor].CGColor;
    righttBottomAddAction.layer.borderWidth = 0.3;
    righttBottomAddAction.titleLabel.font = [UIFont systemFontOfSize:30];
    [self.view addSubview:righttBottomAddAction];
    [righttBottomAddAction mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.view.mas_right).offset(-20);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-20);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(40);
    }];
    
    
    UIButton *centerAddAction = [UIButton buttonWithType:UIButtonTypeCustom];
    [centerAddAction setTitle:@"+" forState:UIControlStateNormal];
    centerAddAction.titleLabel.font = [UIFont systemFontOfSize:15];
    [centerAddAction setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [centerAddAction addTarget:self action:@selector(centerActionClicked:) forControlEvents:UIControlEventTouchUpInside];
    centerAddAction.layer.borderColor = [UIColor grayColor].CGColor;
    centerAddAction.layer.borderWidth = 0.3;
    centerAddAction.titleLabel.font = [UIFont systemFontOfSize:30];
    [self.view addSubview:centerAddAction];
    [centerAddAction mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX).offset(0);
        make.centerY.mas_equalTo(self.view.mas_centerY).offset(0);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(40);
    }];
}

-(void)setupNavItem
{
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"list"] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonItemAction)];
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(leftBarButtonItemAction)];
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(rightBarButtonItemAction:)];
}

#pragma mark - leftBarButtonItem的响应事件
-(void)leftBarButtonItemAction
{
//    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"热更新效果展示" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//    [alertView show];
//    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"热更新效果展示" message:nil preferredStyle:UIAlertControllerStyleAlert];
//    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//
//    }];
//    [alertController addAction:confirmAction];
//    [self presentViewController:alertController animated:YES completion:nil];
}

#pragma mark - rightBarButtonItem的响应事件
-(void)rightBarButtonItemAction:(UIBarButtonItem *)sender
{
    [YCXMenu setTintColor:[UIColor colorWithRed:0.118 green:0.573 blue:0.820 alpha:1]];
    [YCXMenu setSelectedColor:[UIColor redColor]];
    [YCXMenu setBackgroundShadow:YES withColor:nil];
    if ([YCXMenu isShow])
    {
        [YCXMenu dismissMenu];
    }
    else
    {
        [YCXMenu showMenuInView:self.view fromRect:CGRectMake(self.view.frame.size.width - 50, 64, 50, 0) menuItems:self.items selected:^(NSInteger index, YCXMenuItem *item) {
            NSLog(@"%@",item);
        }];
    }
}
#pragma mark - 左上方按钮的响应事件
-(void)leftTopAddActionClicked:(UIButton *)sender
{
    [YCXMenu showMenuInView:self.view fromRect:sender.frame menuItems:self.items selected:^(NSInteger index, YCXMenuItem *item) {
        NSLog(@"%@",item);
    }];
}
#pragma mark - 右上方按钮的响应事件
-(void)rightTopAddActionClicked:(UIButton *)sender
{
    [YCXMenu setHasShadow:YES];
    [YCXMenu setBackgrounColorEffect:YCXMenuBackgrounColorEffectSolid];
    [YCXMenu setTintColor:[UIColor colorWithHexString:@"#ffffff"]];
    [YCXMenu setBackgroundShadow:YES withColor:nil];
    [YCXMenu showMenuInView:self.view fromRect:sender.frame menuItems:[self getRightTopActionDataSource] selected:^(NSInteger index, YCXMenuItem *item) {
        NSLog(@"%@",item);
    }];
}
#pragma mark - 左下方按钮的响应事件
-(void)leftBottomAddActionClicked:(UIButton *)sender
{
    [YCXMenu setHasShadow:YES];
    [YCXMenu setBackgrounColorEffect:YCXMenuBackgrounColorEffectGradient];
    [YCXMenu setTintColor:[UIColor colorWithRed:0.212 green:0.255 blue:0.678 alpha:1]];
    [YCXMenu showMenuInView:self.view fromRect:sender.frame menuItems:self.items selected:^(NSInteger index, YCXMenuItem *item) {
        NSLog(@"%@",item);
    }];
}
#pragma mark - 右下方按钮的响应事件
-(void)rightBottomAddActionClicked:(UIButton *)sender
{
    [YCXMenu setHasShadow:NO];
    [YCXMenu setArrowSize:6];
    [YCXMenu setCornerRadius:2];
    [YCXMenu setBackgrounColorEffect:YCXMenuBackgrounColorEffectSolid];
    [YCXMenu setTintColor:[UIColor colorWithRed:0.212 green:0.255 blue:0.678 alpha:1]];
    [YCXMenu showMenuInView:self.view fromRect:sender.frame menuItems:self.items selected:^(NSInteger index, YCXMenuItem *item) {
        NSLog(@"%@",item);
    }];
}
#pragma mark - 中心按钮的响应事件
-(void)centerActionClicked:(UIButton *)sender
{
    [YCXMenu setHasShadow:YES];
    [YCXMenu setBackgrounColorEffect:YCXMenuBackgrounColorEffectGradient];
    [YCXMenu setTintColor:[UIColor colorWithRed:0.212 green:0.255 blue:0.678 alpha:1]];
    [YCXMenu setSeparatorColor:[UIColor redColor]];
    [YCXMenu setMenuItemMarginY:20];
    [YCXMenu setTitleFont:[UIFont systemFontOfSize:24.0]];
    [YCXMenu showMenuInView:self.view fromRect:sender.frame menuItems:self.items selected:^(NSInteger index, YCXMenuItem *item) {
        NSLog(@"%@",item);
    }];
}

#pragma mark - Notification
- (void)menuWillAppear
{
    NSLog(@"menu will appear");
}

- (void)menuDidAppear
{
    NSLog(@"menu did appear");
}

- (void)menuWillDisappear
{
    NSLog(@"menu will disappear");
}

- (void)menuDidDisappear
{
    NSLog(@"menu did disappear");
}

-(void)logout:(YCXMenuItem *)sender
{
    NSLog(@"点击了退出按钮");
}

-(NSArray *)getRightTopActionDataSource
{
    NSMutableArray *menuItems = [NSMutableArray new];
    NSArray *titleArr = @[@"创建群聊",@"加好友/群",@"扫一扫",@"面对面快传",@"付款",@"拍摄",@"高能舞室"];
    NSArray *imageArr = @[@"file",@"addFriend",@"scan",@"uploadFile",@"payment",@"camera",@"dance"];
    for (NSInteger i = 0; i < titleArr.count; ++i) {
        NSString *titleString = titleArr[i];
        NSString *imageName = imageArr[i%imageArr.count];
        YCXMenuItem *menuTitle = [YCXMenuItem menuTitle:titleString WithIcon:[UIImage imageNamed:imageName]];
        menuTitle.alignment = NSTextAlignmentLeft;
        menuTitle.foreColor = [UIColor blackColor];
        [menuItems addObject:menuTitle];
    }
    NSArray *results = [NSArray arrayWithArray:menuItems];
    return results;
}

- (NSMutableArray *)items {
    if (!_items)
    {
        YCXMenuItem *menuTitle = [YCXMenuItem menuTitle:@"Menu" WithIcon:nil];
        menuTitle.foreColor = [UIColor whiteColor];
        menuTitle.titleFont = [UIFont boldSystemFontOfSize:20.0f];
        YCXMenuItem *logoutItem = [YCXMenuItem menuItem:@"退出" image:nil target:self action:@selector(logout:)];
        logoutItem.foreColor = [UIColor redColor];
        logoutItem.alignment = NSTextAlignmentCenter;
        
        _items = [@[menuTitle,[YCXMenuItem menuItem:@"个人中心" image:nil tag:100 userInfo:@{@"title":@"Menu"}],[YCXMenuItem menuItem:@"ACTION 133" image:nil tag:101 userInfo:@{@"title":@"Menu"}],[YCXMenuItem menuItem:@"检查更新" image:nil tag:102 userInfo:@{@"title":@"Menu"}],logoutItem] mutableCopy];
        
    }
    return _items;
}

- (void)dealloc {
    NSLog(@"销毁界面，移除所有通知");
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
